#include "hidemain.h"
#include <QtGui/QApplication>
#include <QtGui/QIcon>
#include <QtGui/QAction>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QtCore/QDataStream>
#include <QtCore/QSettings>
//#include <QtGui/QCloseEvent>
#include <QtGui/QTextBrowser>
#include <QtGui/QVBoxLayout>

HideMain::HideMain(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
	QSettings settings("APnMEngine.ini", QSettings::IniFormat);
	port = settings.value("port", QVariant(0)).toUInt();

	QPoint pos = settings.value("pos", QPoint(500, 200)).toPoint();
	QSize size = settings.value("size", QSize(500, 400)).toSize();
	move(pos);
	resize(size);

	setWindowIcon( QIcon(":/Resources/logo.png") );

	//------------Info window-------------
	QVBoxLayout* verticalLayout = new QVBoxLayout(this);
	verticalLayout->setSpacing(6);
	verticalLayout->setContentsMargins(11, 11, 11, 11);
	info = new QTextBrowser(this);

	verticalLayout->addWidget(info);
	info->append(tr("Running Engine Protocol and Command Processing:\n"));

	//------------window tray-------------
	tray = new QSystemTrayIcon(this);
	tray->setIcon( QIcon(":/Resources/engine.png") );
	connect(tray, SIGNAL( activated(QSystemTrayIcon::ActivationReason) ),
		    this, SLOT( popMenu(QSystemTrayIcon::ActivationReason) ));
	//tray->setToolTip( tr("This is the Actor Petri net Model Engine!") );

	menu = new QMenu(this);
	QAction* act = new QAction(QIcon(":/Resources/Info.png"), tr("Info..."), this);
	connect(act, SIGNAL(triggered()), this, SLOT(show()));
	menu->addAction(act);
	act = new QAction(QIcon(":/Resources/fly.png"), tr("About..."), this);
	connect(act, SIGNAL(triggered()), this, SLOT(about()));
	menu->addAction(act);
	act = new QAction(QIcon(":/Resources/qt-logo.png"), tr("About Qt..."), this);
	connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
	menu->addAction(act);
	act = new QAction(QIcon(":/Resources/exit.png"), tr("Exit"), this);
	connect(act, SIGNAL(triggered()), qApp, SLOT(quit()));
	menu->addAction(act);
	tray->setContextMenu(menu);
	tray->setVisible(true);
}

HideMain::~HideMain()
{
	//tcpServer->close();
	QSettings settings("APnMEngine.ini", QSettings::IniFormat);
	settings.setValue("port", port);

	settings.setValue("pos", pos());
	settings.setValue("size", size());
}

void HideMain::startServer()
{
	tcpServer = new QTcpServer(this);
	if (!tcpServer->listen(QHostAddress::Any, port)) {
		QMessageBox::critical(this, tr("APnModel Engine Server"),
			tr("Unable to start the server: %1.")
			.arg(tcpServer->errorString()));
		close();
		return;
	}

	port = tcpServer->serverPort();
	menu->insertAction(menu->actions().first(), new QAction(tr("Port %1").arg(port), this) );
	tray->setToolTip(tr("APnModel Engine Server port:%1.")
		             .arg(port));
	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(doService()));
}

void HideMain::doService()
{
	QDataStream out;
	out.setVersion(QDataStream::Qt_4_3);
	
	QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
	connect(clientConnection, SIGNAL(disconnected()),
		clientConnection, SLOT(deleteLater()));

	out.setDevice(clientConnection);
	out << "APnModel Engine Server is running OK on port " << port;
	clientConnection->disconnectFromHost();
}

void HideMain::about()
{
	QMessageBox::about(this, tr("About APnModel-Engine"), tr(
		"<h3>About APnModel-Engine</h3>"
		"<p>APnModel-Engine is an engine which can drive "
		"Scientific Workflow Applications based on <b>Actor Petri net Model</b>.</p>"
		"<p> Actor Petri net Model is a simple model derived from Ptolemy II, "
		"Petri nets, Nested Relational Calculus (maybe also DFL).</p>"
		"<p>Desinger and Developer: Du Nanshan (email: dunanshan@163.com)</p>"
		"<p>Institution: School of Computer, Wuhan University & "
		"Shenzhen Research Institute, City University of Hong Kong<p>"
		"<p>See <a href=\"http://sites.google.com/site/dunanshan\">googleweb/site/dunanshan</a>"
		" and <a href=\"http://code.google.com/p/actor-petri-net-model-ide/\">googlecode/APnModel</a>"
		"for more information.</p>"
		));
}

void HideMain::popMenu(QSystemTrayIcon::ActivationReason reason)
{
	tray->contextMenu()->popup(QCursor::pos());
	tray->contextMenu()->activateWindow();
}